package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.Student;
import com.bluedot.www.core.pojo.DO.User;
import com.bluedot.www.core.pojo.DTO.CustomerDTO;
import com.bluedot.www.core.utils.ExcelUtils;
import com.bluedot.www.core.utils.OSSManageUtil;
import com.bluedot.www.framework.mvc.servlet.http.MultipartFile;
import com.bluedot.www.framework.mvc.servlet.http.ResponseEntity;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 样例
 *
 * @author He Peng
 * @version 1.0
 * @date 2021/8/13 16:13
 */
public class ExampleService {

    /**
     * 构造器私有
     */
    private ExampleService() {
    }


    /**
     * 每个类都必须实现此方法，根据传入的方法名调用其他方法
     *
     * @param methodName: 方法名
     * @param param:      需要调用的方法参数
     * @return java.lang.Object
     * @author He Peng
     * @date 2021/8/21 14:34
     */
    public static Object execute(String methodName, Object[] param) throws IOException, FileUploadException {
        ExampleService exampleService = new ExampleService();
        Object result = null;
        switch (methodName) {
            case "test":
                result = exampleService.test((String) param[0], (User) param[1]);
                break;
            case "test1":
                result = exampleService.test1((String) param[0], (User) param[1], (Long) param[2]);
                break;
            case "testFile":
                result = exampleService.testFile((MultipartFile) param[0]);
                break;
            case "testFiles":
                result = exampleService.testFiles((List<MultipartFile>) param[0]);
                break;
            case "testList":
                result = exampleService.testList((List<Long>) param[0]);
                break;
            case "testList1":
                result = exampleService.testList1((Integer) param[0], (List<Long>) param[1]);
                break;
            case "testList2":
                result = exampleService.testList2((Integer) param[0], (List<User>) param[1]);
                break;
            case "testExcel1":
                result = exampleService.testExcel1((MultipartFile) param[0]);
                break;
            case "testExcel2":
                result = exampleService.testExcel2((Long) param[0]);
                break;
            case "testRequest":
                result = exampleService.testRequest((HttpServletRequest) param[0], (Integer) param[1]);
                break;
            case "testUploadFile":
                result = exampleService.testUploadFile((String) param[0]);
                break;
            case "testUploadImages":
                result = exampleService.testUploadImages((MultipartFile) param[0]);
                break;
            case "testFileAndArg":
                result = exampleService.testFileAndArg((MultipartFile) param[0], (Long) param[1]);
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }

    /**
     * 测试方法
     *
     * @param name:
     * @param user:
     * @return com.bluedot.www.core.pojo.DO.User
     * @author He Peng
     * @date 2021/8/21 14:36
     */
    private User test(String name, User user) {
        user.setName(name);

        String mapperName = "com.bluedot.www.core.mapper.xml.QueryByPageMapper.test";
        Object execute = Execute.execute(Execute.QUERY_TYPE, mapperName, new Object[]{"1"});
        List<User> list = (List<User>) execute;
        User res = list.get(0);
        System.out.println("len : " + list.size() + " " + list.get(0));
        return res;
    }

    private User test1(String name, User user, Long age) {
        user.setName(name);
        user.setId(age);
        return user;
    }


    private String testFile(MultipartFile multipartFile) {
        //测试上传笔记分析图片
        System.out.println("======>" + multipartFile.getName());
//      File file = new File("D:\\" + multipartFile.getOriginalFilename());
        String path = "bluedot/pre_handwriting/";
        String url = null;
        try {
            url = OSSManageUtil.uploadFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), path);
//            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println(url);
        return "上传成功" + url;

    }

    private String testFileAndArg(MultipartFile multipartFile, Long id) {
        System.out.println("id:====>" + id);
        //测试上传笔记分析图片
        System.out.println("======>" + multipartFile.getName());
//      File file = new File("D:\\" + multipartFile.getOriginalFilename());
        String path = "bluedot/pre_handwriting/";
        String url = null;
        try {
            url = OSSManageUtil.uploadFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), path);
//            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println(url);
        return "上传成功" + url;

    }


    private String testFiles(List<MultipartFile> multipartFiles) {
        for (MultipartFile multipartFile : multipartFiles) {
            System.out.println("======>" + multipartFile.getName());
//            File file = new File("D:\\" + multipartFile.getOriginalFilename());
//            try {
//                multipartFile.transferTo(file);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
        return "上传成功";
    }


    private List<Long> testList(List<Long> idList) {
        for (Long aLong : idList) {
            System.out.println(aLong);
        }
        return idList;
    }

    private List<Long> testList1(Integer rt, List<Long> idList) {
        for (Long aLong : idList) {
            System.out.println(aLong);
        }
        System.out.println(rt);
        return idList;
    }


    private List<User> testList2(Integer a, List<User> user) {
        System.out.println("====>" + a);
        for (User user1 : user) {
            System.out.println(user1);
        }
        return user;
    }

    private boolean testExcel1(MultipartFile multipartFile) throws IOException, FileUploadException {
        List<CustomerDTO> customerDTOS = null;
        try {
            customerDTOS = ExcelUtils.excelToCustomerDTO(multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (boolean) ExportService.execute("importCustomerInfo", new Object[]{multipartFile});
    }

    private ResponseEntity testExcel2(Long customerIds) throws IOException, FileUploadException {
        List<Long> longs = new ArrayList<>();
        longs.add(customerIds);
        return (ResponseEntity) ExportService.execute("exportCustomerInfo", new Object[]{longs});
    }

    /**
     * 测试注入request
     *
     * @param req:
     * @param a:
     * @return java.lang.String
     * @author He Peng
     * @date 2021/9/10 9:48
     */
    private String testRequest(HttpServletRequest req, Integer a) {
        System.out.println("session: " + req.getSession().getAttribute("SESSION_KEY_USER"));
        System.out.println("request: " + req + " a: " + a);
        return "request: " + req + " a: " + a;
    }


    /**
     * 测试上传文件到前端
     *
     * @param fileName: 文件名
     * @return com.bluedot.www.framework.mvc.servlet.http.ResponseEntity
     * @author He Peng
     * @date 2021/9/12 15:45
     */
    private ResponseEntity testUploadFile(String fileName) {
        List<Student> list = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            Student studentExcelDto = new Student();
            studentExcelDto.setId(i);
            studentExcelDto.setName("学生" + i);
            studentExcelDto.setEmail(i + "邮箱");
            studentExcelDto.setAge(7 + i);
            list.add(studentExcelDto);
        }

        //将workbook以流的形式直接传到前端下载
        XSSFWorkbook workbook = ExcelUtils.getWorkbook(list, new String[]{"id", "name", "email", "age"}, new String[]{"id", "姓名", "邮箱", "年龄"});
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = os.toByteArray();


//        InputStream in = getClass().getResourceAsStream("/static/backup.sql");
        try {
//            return new ResponseEntity(fileName, in);
            return new ResponseEntity("哈哈.xlsx", bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private ResponseEntity testUploadImages(MultipartFile multipartFile) {
        try {
            InputStream inputStream = multipartFile.getInputStream();
            return new ResponseEntity("1.png", inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
